import os

"""
This little script just makes sure that receptive_field images are unique in a database, prefering the ones found
in lower rf_id folders if duplicates are encountered.
"""

database_path = "/experiment_data/test_database"

import sys

# parse the command line arguements
database_path_passed = False
for arg in sys.argv:
    if database_path_passed:
        database_path = arg
        database_path_passed = False
    if '--dbp' in arg:
        database_path_passed = True
    
rf_path = os.path.join(database_path, "receptive_field_images")

folders = os.listdir(rf_path) # they should all be folders, so no need to check

int_folders = [int(folder) for folder in folders]
int_folders.sort()

for i in xrange(1,len(int_folders)):
    this_folder = os.path.join(rf_path, str(int_folders[i]))
    files = os.listdir(this_folder)
    for file in files:
        this_file = os.path.join(this_folder, file)
        for j in xrange(i):
            prev_folder = os.path.join(rf_path, str(int_folders[j]))
            prev_file = os.path.join(prev_folder, file)
            if os.system('diff "%s" "%s" 2> /tmp/junk >/tmp/junk '% (this_file, prev_file)) == 0:
                print "Found that %s in rf %d and %d are the same." % (file, int_folders[i], int_folders[j])  
                print('removing "%s"' % this_file)
                os.system('rm "%s"' % this_file)
